package com.atguigu.tingshu.account.receiver;


import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: jack
 * @date: 2025/9/23 20:51
 */
@Slf4j
@Component

public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;


    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ACCOUNT, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_USER_REGISTER, durable = "true"),
            key = MqConst.EXCHANGE_USER
    ))
    public void saveUserAccount(Map<String, Object> map, Message message, Channel channel) {
        if (map != null) {
            userAccountService.insertUserAccount(map);
        }
        log.info("用户注册成功,初始化用户账户记录：{},tag:{}", map, message.getMessageProperties().getDeliveryTag());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
